package com.three.service.impl;

import com.three.entity.*;
import com.three.mapper.*;
import com.three.service.OutlibraryGoodsService;
import com.three.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 袁大毛
 * * @date 2024/10/29 上午9:18
 * @描述
 */
@Service
public class OutlibraryGoodsServiceImpl implements OutlibraryGoodsService {
    @Autowired
    private OutlibraryGoodsMapper outlibraryGoodsMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private OutlibraryMapper outlibraryMapper;
    @Autowired
    private StorageVolumeMapper storageVolumeMapper;
    @Autowired
    private BranchMapper branchMapper;

    @Override
    public R getOutlibraryGoods() {
        List<Map<Object, Object>> xiaoLiang = outlibraryGoodsMapper.getXiaoLiang();
        //System.out.println(xiaoLiang+"完成");
        List<Object> namelist=new ArrayList<>();
        List<Object> sumlist =new ArrayList<>();

        for (Map<Object, Object> list1 : xiaoLiang) {
            Object goodsid = list1.get("goodsname");
            Object num = list1.get("num");
            namelist.add(goodsid);
            sumlist.add(num);
        }
        //System.out.println(nameidlist+"商品id集合");
        //System.out.println(sumlist+"商品数量集合");
        Map<String, Object> map=new HashMap<>();
        map.put("namelist",namelist);
        map.put("sumlist",sumlist);
        //System.out.println(map);
        return R.ok(map);
    }
    //根据当前门店查询出来，当前门店亏损多少，盈利多少
    @Override
    public R getByBranchIdYingLi() {
        //得到当前门店
        Integer branchid = ((Staff) session.getAttribute("staff")).getBranchid();
        //查询当前门店的所拥有的商品数目，已经这些商品的进价，售价,该门店的所有商品
        List<GoodsNumJinShouVo> goodsAllGoods = goodsMapper.selectGoodsShuJinSou(branchid);
        //System.out.println(goodsAllGoods);
        //计算这些商品的进价
        int jinzong =0;
        for (GoodsNumJinShouVo goodsNumJinShouVo : goodsAllGoods){
            //当前商品的进价
            int a = goodsNumJinShouVo.getCounts()*goodsNumJinShouVo.getCprice();
            //总价
            jinzong +=a;
        }
        //查询每个商品的出库数量，计算盈利
        Integer zong=0;
        for (GoodsNumJinShouVo aaa : goodsAllGoods){
            Integer goodsid = aaa.getGoodsid();
            //查询商品的出库数量
            List<Integer> outGoodsCounts = outlibraryGoodsMapper.getOutGoodsCounts(goodsid,branchid);
            //计算该商品的盈利
            for (Integer num : outGoodsCounts){
                zong=num*aaa.getSprice()+zong;
            }
        }
        int zhuan=zong-jinzong;
        List<GoodsNumJinShouFanVo> list=new ArrayList<>();
        GoodsNumJinShouFanVo chengben=new GoodsNumJinShouFanVo();
        chengben.setName("成本");
        chengben.setValue(jinzong);
        GoodsNumJinShouFanVo yingyee=new GoodsNumJinShouFanVo();
        yingyee.setName("盈利");
        yingyee.setValue(zong);
        GoodsNumJinShouFanVo kunsun=new GoodsNumJinShouFanVo();
        kunsun.setName("亏损");
        kunsun.setValue(zhuan);
        list.add(chengben);
        list.add(yingyee);
        //list.add(kunsun);
        return R.ok(list);
    }

    @Override
    public R getByBranchYingLiE() {
        //查询所有门店的门店编号和门店名称
        List<Branch> branchAll= branchMapper.getAll();
        List<BranchYingYeEVo> abc=new ArrayList<>();
        for (Branch branch : branchAll){
            //得到门店编号
            Integer branchid = branch.getBranchid();
            //得到门店名称
            String name = branch.getBranchname();
            //调用方法
            BranchYingYeEVo branchYingYeEVo = suanBranchTurnover(branchid,name);
            abc.add(branchYingYeEVo);
        }
        //System.out.println(abc);
        return R.ok(abc);
    }

    @Override
    public R loadtu01(TimeVo timeVo) {
        //得到当前门店
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();//门店编号
        timeVo.setBranchid(branchid);

        //查出该门店下通过出库审核的商品
        List<OutlibraryGoods> outlibraryAll = outlibraryGoodsMapper.outlibraryAllByBranchid(timeVo);
        //求出门店营业额
        int chusum=0;
        //得到每条的total
        for (OutlibraryGoods outlibraryGoods : outlibraryAll) {
            chusum += outlibraryGoods.getTotal();
        }


        //成本额
        int chengben=0;
        //查询该门店下的入库商品
        List<StorageVolume> storageVolumes = storageVolumeMapper.selectByBranchid(timeVo);

        for (StorageVolume storageVolume : storageVolumes) {
            Integer goodsid = storageVolume.getGoodsid();

            //查询商品的进价
            Goods goods = goodsMapper.selectBygoodsid(goodsid);
            if (goods!=null){

                Double cprice = goods.getCprice();

                //查询商品的数量
                Integer counts = storageVolume.getNum();

                //计算成本额
                chengben += cprice*counts;
            }else {
                continue;
            }
        }

        //如果营业额大于成本额，则利润为营业额-成本额，如果营业额小于成本额，则亏损为成本额-营业额
        int zhuan=chusum-chengben;
        int kuisun=chengben-chusum;
        if (zhuan>=0){
            kuisun=0;
        }else {
            zhuan=0;
        }
        List<Object> list=new ArrayList<>();
        list.add(chusum);
        list.add(chengben);
        list.add(zhuan);
        list.add(kuisun);

        return R.ok(list);
    }

    @Override
    public R loadtu02(TimeVo timeVo) {
        //获取所有的店铺id
        List<Branch> all = branchMapper.getAll();
        //循环调用店铺id查询门店营业额
        List<Map<String, Object>> list=new ArrayList<>();
        for (Branch branch : all) {
            //得到店铺id
            Integer branchid = branch.getBranchid();
            timeVo.setBranchid(branchid);
            //得到店铺名称
            String branchname = branch.getBranchname();
            //查出该门店下通过出库审核的商品
            List<OutlibraryGoods> outlibraryAll = outlibraryGoodsMapper.outlibraryAllByBranchid(timeVo);
            //求出门店营业额
            int chusum=0;
            //得到每条的total
            for (OutlibraryGoods outlibraryGoods : outlibraryAll) {
                chusum += outlibraryGoods.getTotal();
            }
            //封装
           Map<String,Object> map=new HashMap<>();
            map.put("name",branchname);
            map.put("value",chusum);
            list.add(map);
        }
        return R.ok(list);
    }

    @Override
    public R loadtu03(TimeVo timeVo) {
        //得到当前门店
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        timeVo.setBranchid(branchid);
        //查询当前门店的所有出库商品
        List<OutlibraryGoods> outlibraryAll = outlibraryGoodsMapper.AllByGroupgoodsid(timeVo);
        //封装
        Map<Object, Object> map=new HashMap<>();
        List<String> names = new ArrayList<>();
        List<Integer> nums = new ArrayList<>();
        for (OutlibraryGoods outlibraryGoods : outlibraryAll) {
            Integer goodsid = outlibraryGoods.getGoodsid();//商品id
            Goods goods = goodsMapper.selectBygoodsid(goodsid);
            String goodsname = goods.getGoodsname();//商品名称
            Integer num = outlibraryGoods.getNum();//出库数量
            names.add(goodsname);
            nums.add(num);
        }
        map.put("name",names);
        map.put("value",nums);
        return R.ok(map);
    }

    @Override
    //查询门店名称
    public R loadBranchName() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Branch userBranchName = branchMapper.getUserBranchName(branchid);
        return R.ok(userBranchName.getBranchname());
    }

    /**
     * 查询门店营业额  方法
     * @param branchid
     * @param name
     * @return
     */
    public BranchYingYeEVo suanBranchTurnover(Integer branchid,String name){
        //查询当前门店的所拥有的商品数目，已经这些商品的进价，售价,该门店的所有商品
        List<GoodsNumJinShouVo> goodsAllGoods = goodsMapper.selectGoodsShuJinSou(branchid);
        //System.out.println(goodsAllGoods);
        //计算这些商品的进价
        int jinzong =0;
        for (GoodsNumJinShouVo goodsNumJinShouVo : goodsAllGoods){
            //当前商品的进价
            int a = goodsNumJinShouVo.getCounts()*goodsNumJinShouVo.getCprice();
            //总价
            jinzong +=a;
        }
        //查询每个商品的出库数量，计算盈利
        Integer zong=0;
        for (GoodsNumJinShouVo aaa : goodsAllGoods){
            Integer goodsid = aaa.getGoodsid();
            //查询商品的出库数量
            List<Integer> outGoodsCounts = outlibraryGoodsMapper.getOutGoodsCounts(goodsid,branchid);
            //计算该商品的盈利
            for (Integer num : outGoodsCounts){
                zong=num*aaa.getSprice()+zong;
            }
        }
        int zhuan=zong-jinzong;
        BranchYingYeEVo branchYingYeEVo=new BranchYingYeEVo();
        branchYingYeEVo.setName(name);
        branchYingYeEVo.setValue(zong);
        return branchYingYeEVo;
//        System.out.println(map);
    }
}